package com.yinbo.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	private DateUtils() {}

    /************************
     *  :: UTILITY METHODS ::
     ************************/

    /**
     *  Returns a static Date instance
     *
     *  @param  dateStr - String     date string
     *  @param  dateFormatStr - String   date format
     */
    public static Date stringToDate( String dateStr, String dateFormatStr ) {
        if ( dateStr == null || dateStr.trim().length() == 0 ||
             dateFormatStr == null || dateFormatStr.trim().length() == 0 ) { return null; }

        SimpleDateFormat sdf = new SimpleDateFormat( dateFormatStr );
        return sdf.parse( dateStr, new ParsePosition( 0 ) );

    }// end stringToDate()

    /**
     *  Returns a static String representation of the given date and date format
     *
     *  @param  date - Date
     *  @param  dateFormatStr - String   date format
     */
    public static String dateToString( Date date, String dateFormatStr ) {
        if ( date == null || dateFormatStr == null || dateFormatStr.trim().length() == 0 ) { return null; }

        SimpleDateFormat sdf = new SimpleDateFormat( dateFormatStr );
        return sdf.format( date );

    }// end dateToString()

    /**
     *  Compares two date strings by the given format string and returns a boolean
     *  based upon begin date string is before end date string
     *
     *  @param  beginDateStr - String
     *  @param  endDateStr - String
     *  @param  dateFormatStr - String
     *  @return boolean
     */
    public static boolean isBefore( String beginDateStr, String endDateStr, String dateFormatStr ) {
        Date beginDate = stringToDate( beginDateStr, dateFormatStr );
        Date endDate = stringToDate( endDateStr, dateFormatStr );
        if ( beginDate == null || endDate == null ) { return false; }

        return isBefore( beginDate, endDate );

    }// isBefore()

    /**
     *  Compares two java.util.Date objects and returns a boolean
     *  based upon beginDate is before endDate
     *
     *  @param  beginDate - java.util.Date
     *  @param  endDate - java.util.Date
     *  @return boolean
     */
    public static boolean isBefore( Date beginDate, Date endDate ) {
        if ( beginDate == null || endDate == null ) { return false; }
        GregorianCalendar calBegin = new GregorianCalendar();
        calBegin.setTime( beginDate );
	    GregorianCalendar calEnd = new GregorianCalendar();
        calEnd.setTime( endDate );

        return calBegin.before( calEnd );

    }// isBefore()

    /**
     *  Returns a java.util.Calendar in XSD Date format
     *  @param  date - java.util.Calendar
     *  @return String representing XSD Date format
     */
	public static String getDateAsXSDDate(java.util.Calendar date ) {

		if ( date == null) { return "xxxx-xx-xx"; }
		String year = java.lang.Integer.toString( date.get(java.util.Calendar.YEAR) );
		String month = ( date.get(java.util.Calendar.MONTH) + 1) < 10
		? "0" + java.lang.Integer.toString( date.get(java.util.Calendar.MONTH) + 1 )
		: java.lang.Integer.toString( date.get(java.util.Calendar.MONTH) + 1 );
		String day = ( date.get(java.util.Calendar.DAY_OF_MONTH) ) < 10
		? "0" + java.lang.Integer.toString( date.get(java.util.Calendar.DAY_OF_MONTH) )
		: java.lang.Integer.toString( date.get(java.util.Calendar.DAY_OF_MONTH) );

		return year + "-" + month + "-" + day;
	}
	
	/**
	 * 转化为yyyy-MM-dd HH:mm:ss
	 * @param tempDateTime
	 * @return
	 */
	 public static String getTimeStr(Date tempDateTime) {
			String moedifiedTime = null;
			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				moedifiedTime = new String(dateFormat.format(tempDateTime));
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
			return moedifiedTime;
		}
	 
	 /**
	  * 转化为yyyy-MM-dd
	  * @param tempDateTime
	  * @return
	  */
	 public static String getTimeStrOne(Date tempDateTime) {
			String moedifiedTime = null;
			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyy-MM-dd");
				moedifiedTime = new String(dateFormat.format(tempDateTime));
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
			return moedifiedTime;
		}

	
}
